package com.zzyl.serve.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingProjectMapper;
import com.zzyl.serve.domain.NursingProject;
import com.zzyl.serve.service.INursingProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理项目Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-30
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    // 常量:缓存的key-全查护理项目表
    private static final String CACHE_KEY_PROJECT_ALL = "nursingProject:all";

    /**
     * 查询护理项目
     *
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id) {
        return getById(id);
    }

    /**
     * 查询护理项目列表
     *
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject) {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject) {

        // 先更新数据库
        boolean flag = save(nursingProject);
        // 再删除缓存
        redisTemplate.delete(CACHE_KEY_PROJECT_ALL);
        return flag ? 1 : 0;
    }

    /**
     * 修改护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject) {
        // 先更新数据库
        boolean flag = updateById(nursingProject);
        // 再删除缓存
        redisTemplate.delete(CACHE_KEY_PROJECT_ALL);
        return flag ? 1 : 0;
    }

    /**
     * 批量删除护理项目
     *
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids) {
        // 先判断状态是否为启用,为启用不能删
        List<NursingProject> nursingProjects = this.listByIds(Arrays.stream(ids).collect(Collectors.toList()));
        if (!CollectionUtils.isEmpty(nursingProjects)) {
            nursingProjects.forEach(nursingProject -> {
                if (nursingProject.getStatus() == 1) {
                    throw new RuntimeException("启用的护理项目不能删除!");
                }
            });
        }
        // 先更新数据库
        boolean flag = removeByIds(Arrays.asList(ids));
        // 再删除缓存
        redisTemplate.delete(CACHE_KEY_PROJECT_ALL);
        return flag ? 1 : 0;
    }

    /**
     * 删除护理项目信息
     *
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id) {
        // 判断状态是否为启用,为启用不能删
        NursingProject nursingProject = getById(id);
        if (nursingProject.getStatus() == 1) {
            throw new RuntimeException("启用的护理项目不能删除!");
        }
        // 先更新数据库
        boolean flag = removeById(id);
        // 再删缓存
        redisTemplate.delete(CACHE_KEY_PROJECT_ALL);
        return flag ? 1 : 0;
    }


    /**
     * 查询所有护理项目
     *
     * @return
     */
    @Override
    public List<NursingProjectVo> findAll() {
        // 先从缓存中查
        List<NursingProjectVo> result = (List<NursingProjectVo>) redisTemplate.opsForValue().get(CACHE_KEY_PROJECT_ALL);
        // 缓存中有,直接返回
        if (!CollectionUtils.isEmpty(result)) {
            return result;
        }
        // 缓存中没有,查数据库,并给缓存赋值
        result = nursingProjectMapper.findAll();
        redisTemplate.opsForValue().set(CACHE_KEY_PROJECT_ALL, result);
        return result;
    }
}
